package com.kkb.controller;


import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Drug;
import com.kkb.service.DrugService;
import com.kkb.vo.DrugQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("drug")
public class DrugController {

    @Resource
    DrugService service;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO queryAll() {
        List result = service.queryAll();
        return result == null ? new ResultVO<>(500, "服务器异常") : new ResultVO<>(result);
    }

    @RequestMapping(value = "page",method = RequestMethod.GET)
    public ResultVO selectInPage(Integer index, Integer size , DrugQueryVO vo){
        PageInfo<Drug> drugs = service.selectInPage(index,size,vo);
        return drugs!=null ? new ResultVO(drugs): new ResultVO(404,"暂无数据");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO selectById(@PathVariable("id") Integer id){
        Drug drug = service.selectById(id);
        return drug!=null?new ResultVO(drug):new ResultVO(404,"未找到资源");
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO insertDrug(Drug drug){
        return service.insertDrug(drug)>0 ? new ResultVO<>(): new ResultVO(500,"该编号已存在");
    }

    @RequestMapping(value = "stock/{id}",method = RequestMethod.PUT)
    public ResultVO updateStock(@PathVariable("id") Integer id, Integer increase){
        return service.updateStock(id, increase) > 0 ?
                new ResultVO(): new ResultVO(500,"添加库存失败，服务器异常");
    }
}
